package com.example.doctor.mapper;

import com.example.doctor.dto.req.DiagnosisInReq;
import com.example.doctor.dto.resp.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
public interface PatientMapper {
    @Select("SELECT " +
            "orx.rx_order_no_nvarchar AS RxOrderNO ,orx.create_time_datetime AS CreateTime,orx.dialectical_nvarchar " +
            "AS Dialectical " +
            "FROM order_rx_main orx " +
            "INNER JOIN order_main om " +
            "ON(orx.order_no_nvarchar=om.order_no_nvarchar) " +
            "AND orx.d_type_tinyint=2 " +
            "AND orx.system_status_tinyint=2 " +
            "AND om.pid_bigint=#{pid} "+
            "AND orx.is_trial_pharm_ca_bit=true "+
            "ORDER BY orx.create_time_datetime DESC")
    List<xRxListRsp> queryByPid(@Param("pid") Long pid);


    @Select("SELECT" +
            " rx_order_no_nvarchar AS RxOrderNO,drug_name_nvarchar AS DrugName,spec_nvarchar AS Spec,num_int " +
            "AS Num,med_way_nvarchar AS MedWay,freq_nvarchar AS Freq,does_int AS Does,does_unit_nvarchar " +
            "AS DoesUnit,day_int AS Day,remark_nvarchar AS Remark,num_int as Num " +
            "FROM order_rx_drugs " +
            "WHERE rx_order_no_nvarchar =#{rxOrderNO}")
    List<Drug> queryByRxOrderNO(@Param("rxOrderNO") String rxOrderNO);

    @Select("SELECT " +
            "orx.rx_order_no_nvarchar AS RxOrderNO ,orx.d_type_tinyint as DType,orx.create_time_datetime AS CreateTime,orx.dialectical_nvarchar AS " +
            "Dialectical,ori.drug_info_nvarchar AS DrugInfo,ori.does_int AS Does,ori.day_count_int AS DayCount," +
            "ori.avoid_nvarchar AS Avoid,ori.use_time_nvarchar AS UseTime,ori.cook_nvarchar AS Cook,ori.total_does_int " +
            "AS TotalDoes,ori.med_way_nvarchar AS MedWay   " +
            "FROM order_rx_main orx INNER JOIN order_main om " +
            "ON(orx.order_no_nvarchar=om.order_no_nvarchar) " +
            "INNER JOIN order_rx_info ori " +
            "ON(orx.rx_order_no_nvarchar=ori.rx_order_no_nvarchar) " +
            "WHERE orx.d_type_tinyint in (1,3) " +
            "AND orx.system_status_tinyint =2 " +
            "AND orx.is_trial_pharm_ca_bit=true "+
            "AND om.pid_bigint =#{pid} " +
            "ORDER BY orx.create_time_datetime DESC ")
    List<zRxListRsp> queryByRxOrderNOz(@Param("pid") Long pid);


    @Select("SELECT " +
            "c.`name` AS Name,c.title AS Title,d.name_nvarchar AS dName,a.receive_time_datetime AS ReceiveTime ," +
            " a.order_no_nvarchar AS OrderNO ,b.first_tinyint AS First " +
            "FROM order_main a " +
            "INNER JOIN order_patients_info b " +
            "ON(a.order_no_nvarchar=b.order_no_nvarchar) " +
            "INNER JOIN doctor_base_info c " +
            "ON (a.dc_id_bigint=c.dc_id) " +
            "INNER JOIN h_hospital_department d " +
            "ON(c.depart_id=d.id_bigint) " +
            "WHERE a.pid_bigint=#{pid} " +
            "AND a.system_status_tinyint=2 " +
            "AND oc_status_tinyint !=0 " +
            "AND a.receive_time_datetime>#{dateTime}  " +
            "ORDER BY a.receive_time_datetime DESC ")
    List<DiagnosisListRsp> queryDiagnosisListByPid(@Param("pid") Long pid, @Param("dateTime") Date dateTime);


    @Select("SELECT " +
            "a.name_nvarchar AS Name,a.sex_nvarchar AS Sex,a.age_int AS Age,c.`name` AS dName ,c.title AS Title,d.name_nvarchar AS depName,b.receive_time_datetime AS ReceiveTime,a.chief_complaint_nvarchar AS Chiefcomplaint,a.allergy_nvarchar AS Allergy,a.past_disease_nvarchar AS PastDisease " +
            "FROM order_patients_info a " +
            "INNER JOIN order_main b " +
            "ON(a.order_no_nvarchar=b.order_no_nvarchar) " +
            "INNER JOIN doctor_base_info c " +
            "ON(b.dc_id_bigint=c.dc_id) " +
            "INNER JOIN h_hospital_department d " +
            "ON (c.depart_id=d.id_bigint) " +
            "WHERE a.order_no_nvarchar =#{orderNO}")
    DiagnosisInRsp DiagnosisIn(@Param("orderNO") String orderNO);

    @Select("SELECT " +
            "a.name_nvarchar AS Name,e.id_no_nvarchar as IdCard,a.sex_nvarchar AS Sex,a.age_int AS Age,b.a_type_int AS AType,c.`name` AS dName,"+
            "c.title AS Title,d.name_nvarchar AS depName,b.receive_time_datetime AS ReceiveTime ,"+
            "a.chief_complaint_nvarchar AS Chiefcomplaint,a.allergy_nvarchar AS Allergy,a.past_disease_nvarchar AS PastDisease,"+
            "a.f_visit_date_date AS FVisitDate,a.f_hospital_nvarchar AS FHospital,a.f_department_nvarchar AS FDepartment,"+
            "a.f_diagnosis_nvarchar AS FDiagnosis,a.id_bigint AS ID,a.first_tinyint as firstType,"+
            "a.is_pregnancy_bit as isPregnancy,a.is_lactation_period_bit as isLactationPeriod " +
            "FROM order_patients_info a " +
            "INNER JOIN order_main b " +
            "ON(a.order_no_nvarchar=b.order_no_nvarchar) " +
            "INNER JOIN doctor_base_info c " +
            "ON(b.dc_id_bigint=c.dc_id) " +
            "INNER JOIN h_hospital_department d " +
            "ON(c.depart_id=d.id_bigint) " +
            "INNER JOIN client_patients_info e " +
            "ON(e.pid_bigint=a.pid_bigint) " +
            "WHERE a.order_no_nvarchar=#{orderNO}")
    DiagnosisOutRsp DiagnosisOut(@Param("orderNO") String orderNO);

    @Select("select b.hx_id_nvarchar as HXID,b.header_nvarchar as Header"
    +" from client_patients_info a"
    +" inner join client_base_info b on a.client_id_bigint = b.uid_bigint"
    +" where a.pid_bigint = #{PID}")
    Map<String, Object> ClientInfo(@Param("PID") long PID);

    @Select("select b.group_name_nvarchar from doctor_group_patients a"
    +" inner join doctor_group_main b on a.gid_bigint = b.gid_bigint"
    +" where a.pid_bigint = #{PID} and b.dc_id_bigint = #{DCID}"
    +" LIMIT 1")
    String getGroupName(@Param("PID") long PID, @Param("DCID") long DCID);

}
